
package com.njkwt.business.system.service;

import com.njkwt.business.system.domain.Relevance;
import com.njkwt.business.system.domain.RelevanceExample;
import com.njkwt.business.system.dto.RelevanceDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.mapper.RelevanceMapper;
import com.njkwt.util.CopyUtil;
import com.njkwt.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

/**
* @ClassName  RelevanceService
* @Description
* @Author ydj (171799383@qq.com)
* @Date 2020-8-18 14:12:32
* @Version V1.0
**/

@Service
public class RelevanceService {

    @Resource
    private RelevanceMapper relevanceMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        RelevanceExample relevanceExample = new RelevanceExample();
        List<Relevance> relevanceList = relevanceMapper.selectByExample(relevanceExample);
        PageInfo<Relevance> pageInfo = new PageInfo<>(relevanceList);
        pageDto.setTotal(pageInfo.getTotal());
        List<RelevanceDto> relevanceDtoList = CopyUtil.copyList(relevanceList, RelevanceDto.class);
        pageDto.setList(relevanceDtoList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(RelevanceDto relevanceDto) {
        Relevance relevance = CopyUtil.copy(relevanceDto, Relevance.class);
        if (StringUtils.isEmpty(relevanceDto.getId())) {
            this.insert(relevance);
        } else {
            this.update(relevance);
        }
    }

    /**
     * 新增
     */
    private void insert(Relevance relevance) {
        Date now = new Date();
        relevance.setId(UuidUtil.getShortUuid());
        relevanceMapper.insert(relevance);
    }

    /**
     * 更新
     */
    private void update(Relevance relevance) {
        relevanceMapper.updateByPrimaryKey(relevance);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        relevanceMapper.deleteByPrimaryKey(id);
    }
}
